#coding=utf8
from . import user
import json
from flask import render_template, request, url_for, jsonify, Response
from ..models import Activity
from ..utils import parse
from ..models import db
import os
from .. import app

@user.route('/hello')
def hello():
	return 'hello'

@user.route('/carousel')
def carousel():
	data = {
		'rm': 'success',
		'rc': '200',
		'data': {
			'title': '',
			'duedate': '',
			'imgUrl': '',
			'heat': 100
		}
	}
	return json.dumps(data)

@user.route('/details')
def details():
	article_id = request.values.get('id')
	print article_id
	# print render_template('js_article2.html')

	activity = Activity.query.filter_by(id=article_id).first()
	if activity is not None:
		activity.count += 1
		db.session.commit()

	filepath = os.path.join(app.config['WECHAT_HTML'], activity.id+'.html')

	article_title, article_date, article_source, article_detail = parse(filepath)
	data = {
		"article_title": article_title,
		"article_date": article_date,
		"article_source": article_source,
		"article_detail": article_detail,
	}
	return jsonify(data)

@user.route('/contents')
def contents():
	return render_template('js_content.html')

@user.route("/img/<imageid>")
def index(imageid):
	filepath = "./app/static/uploads/{}".format(imageid)
	image = file(filepath)
	resp = Response(image, mimetype="image/jpeg")
	return resp

@user.route('/home', methods=['GET', 'POST'])
def home():
	chid = request.values.get('chid')
	page = request.values.get('page')
	# print 'chid', chid
	# print 'page', page

	if (chid is None) or (page is None):
		error = {
			'code': 1,
			'msg': 'parameter is error'
		}
		return jsonify(error)

	news_page_data = Activity.query.filter_by(
		new_state=2
	).filter_by(
		new_activity_class=int(chid)
	).order_by(
		Activity.adding_time.desc()
	).paginate(page=int(page)+1, per_page=12)


	newsList = []
	for activity in news_page_data.items:
		news = {
			"id": activity.id,
			"title": activity.title,
			"image": '/user/img/' + activity.logo,
			"deadLine": activity.closing_date,
			"group": activity.organization_name,
			"date": activity.adding_time,
			"hotNum": activity.count
		}
		newsList.append(news)
	# print newsList


	# newsList = [
	# 	{
     #        "id": '10',
     #        "title": '第九届校园十佳歌手比赛',
     #        "image": 'http://39.107.109.49:3389/user/img/201803231946349c1542a502704538819608f2bed0174a.jpg',
     #        "deadLine": '截止日期：2018-1-23',
     #        "group": '软微研究生会',
     #        "date": '2017-12-1',
     #        "hotNum": '300'
     #    }
	# ]
	# newsList = newsList * 12

	banner_data = Activity.query.filter_by(
		new_state=2
	).order_by(
		Activity.count
	).limit(4).all()

	newsBanner = []
	for activity in banner_data:
		news = {
			"id": activity.id,
			"title": activity.title,
			"image": '/user/img/' + activity.logo,
			"deadLine": activity.closing_date,
			"group": activity.organization_name,
			"date": activity.adding_time,
			"hotNum": activity.count
		}
		newsBanner.append(news)

	# newsBanner = [
	# 	{
     #        "id": '3',
     #        "title": '第九届校园十佳歌手比赛',
     #        "image": 'http://39.107.109.49:3389/user/img/201803231947237768a236be25415092ec5741e2f7a517.jpg',
     #        "deadLine": '截止日期：2018-1-23',
     #        "hotNum": '300'
     #    }
	# ]
	# newsBanner = newsBanner * 4

	data = {
		"code": 0,
		"newsList": newsList,
		"newsBanner": newsBanner
	}

	# print data

	return jsonify(data)